<?php
/**
 * Created by Sublime.
 * @Author lingyun 
 * @Time:  2017-11-21 17:56:16
 */
namespace app\store\controller;
use controller\BasicAdmin;
use service\DataService;
use think\Db;
use app\store\service\RefundService;
class Saleorder1 extends BasicAdmin{
	/**
	 * 订单列表页面
	 * @Author   lingyun
	 * @DateTime 2017-11-21T19:12:15+0800
	 * @return   [type]                   [description]
	 */
	public function index(){
		$table = "Order";
		$this->title = "订单管理";
		$get = $this->request->get();
		$store_id = session('user')['store_id'];
		isset($get['type'])?$get['type']=$get['type']:$get['type']=1;		 
		if(isset($get['type']) && $get['type'] == 1){	//线上订单
			$map = ['a.store_id'=>['eq',$store_id],'a.order_type'=>['eq',1]];
			$map1 = ['a.store_id'=>['eq',$store_id],'a.order_status'=>['eq',1],'a.order_type'=>['eq',1]];		
		}else{	//线下订单	
			$map = ['a.store_id'=>['eq',$store_id],'a.order_type'=>['eq',0]];
			$map1 = ['a.store_id'=>['eq',$store_id],'a.order_status'=>['eq',1],'a.order_type'=>['eq',0]];				
		}
		$join = [
			['ys_user b','b.uid=a.uid','LEFT'],
			['ys_order_details c','a.id=c.order_id','LEFT']
		];
		$db = Db::name('order')->alias('a')->join($join)->where($map)->field('a.id,a.pay_type,a.pay_status,a.goods_num,a.final_amount,a.order_type,a.order_status,a.order_sn,b.realname,b.mobile,sum(c.is_tuihuo) as total,(select count(*) from ys_order_details where order_id=a.id and th_deal_result>0) as count')->order('a.id desc')->group('c.order_id');
		$new_order = Db::name('order')->alias('a')->where($map1)->count();		//新订单的个数
        foreach (['order_sn'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.order_sn', "%{$get[$key]}%");
        }
        if(isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            $start = $start."  00:00:00";		$end = $end." 23:59:59";
            $start = strtotime($start);			$end = strtotime($end);
            $db->whereBetween('a.addtime',["{$start}", "{$end}"]);
        }
        $this->assign('new_order',$new_order);		//新订单的个数
        return parent::_list($db);
	}	
	/**
	 * 未处理订单列表
	 * @Author   lingyun
	 * @DateTime 2017-11-27T09:42:28+0800
	 * @return   [type]                   [description]
	 */
	public function new_order(){
		$table = "Order";
		$this->title = "未处理订单";
		$get = $this->request->get();
		$store_id = session('user')['store_id'];
		$join = [
			['ys_user b','b.uid=a.uid','LEFT'],
			['ys_order_details c','a.id=c.order_id','LEFT']
		];
		$db = Db::name('order')->alias('a')->join($join)->where(['a.store_id'=>$store_id,'a.order_status'=>1])->field('a.id,a.pay_type,a.pay_status,a.goods_num,a.final_amount,a.order_type,a.order_status,a.order_sn,b.realname,b.mobile,sum(c.is_tuihuo) as total,(select count(*) from ys_order_details where order_id=a.id and th_deal_result>0) as count')->order('a.id desc')->group('c.order_id');
        foreach (['order_sn'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike('a.id', "%{$get[$key]}%");
        }
        if(isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            $start = $start."  00:00:00";		$end = $end." 23:59:59";
            $start = strtotime($start);			$end = strtotime($end);
            $db->whereBetween('a.addtime',["{$start}", "{$end}"]);
        }
        return parent::_list($db);
	}
	/**
	 * 订单详情
	 * @Author   lingyun
	 * @DateTime 2017-11-22T08:41:43+0800
	 * @return   [type]                   [description]
	 */
	public function detail(){
		$id = input('id');		//订单id   20171123.。。。。。
		$join = [
			['ys_goods b','a.goods_id=b.id','LEFT'],
			['ys_order c','a.id=c.id','LEFT'],
		];
		//下单人信息以及订单信息
		$info = Db::name('user')->alias('d')->join('ys_order c','d.uid=c.uid','LEFT')->field('d.realname,d.mobile,d.uid,d.email,c.consignee,c.address,c.zipcode,c.id,c.order_sn,c.final_amount,c.order_status,c.addtime,c.pay_time,c.pay_type,c.score,c.goods_price,c.dis_type,c.discount,c.user_note,c.admin_note,c.coupon_price,c.order_type')->where('c.id',$id)->find();

		//订单操作记录
		$action = Db::name('order_action')->alias('f')->join('ys_system_user e','f.action_user=e.id','LEFT')->field('f.*,e.username')->where('f.order_id',$id)->order('f.id asc')->select();		//订单action

		// halt($id);
		//订单商品详情
		$db = Db::name('order_details')->alias('a')->join($join)->where('a.order_id',$id)->field('a.id,a.spec_detail,a.goods_num,a.signal_price,a.status,a.is_tuihuo,a.tuihuo_sign,a.tuihuo_num,a.th_deal_result,a.th_deal_time,a.th_deal_remark,b.addtime,b.name,c.order_type,c.pay_status,c.pay_type,c.final_amount,c.pay_time,c.id as order_id,c.consignee,c.country,c.province,c.city,c.district,c.twon,c.address,c.mobile,c.addtime as xiadan_time')->order('a.id desc');

		$this->assign('info',$info);
		$this->assign('action',$action);
        return parent::_list($db);
	}
	/**
	 * 退货原因
	 * @Author   lingyun
	 * @DateTime 2017-11-23T16:37:48+0800
	 * @return   [type]                   [description]
	 */
	public function back(){
		$id = input('id');
		$info = Db::name('order_details')->alias('a')->join('goods b','a.goods_id=b.id','LEFT')->join('ys_order c','a.order_id=c.id','LEFT')->where('a.id',$id)->field('a.id,a.spec_detail,a.tuihuo_time,a.reason,a.img,a.tuihuo_sign,a.tuihuo_num,a.tuihuo_amount,a.th_deal_result,a.th_deal_remark,a.th_deal_time,b.name,c.id as order_id,c.pay_type,c.order_type')->find();
		$this->assign('info',$info);
		$this->assign('title','退货原因');
		return $this->fetch();
	}
	/**
	 * 调整价格
	 * @Author   lingyun
	 * @DateTime 2017-11-23T15:12:09+0800
	 * @return   [type]                   [description]
	 */
	public function discount(){
		$table = "Order";
        return $this->_form($table,'discount');
	}
	/**
	 * 余额
	 * @Author   lingyun
	 * @DateTime 2017-11-23T16:05:51+0800
	 * @return   [type]                   [description]
	 */
	public function refund(){
		if(request()->isPost()){
			$post = input('post.');				//接受的处理数据
			$service = new RefundService();		//服务层
			$order = Db::name('order_details')->alias('a')->join('ys_order b','a.order_id=b.id','LEFT')->where('a.id',$post['id'])->field('a.order_id,a.goods_id,a.spec_id,a.spec_detail,a.tuihuo_sign,a.give_score,a.tuihuo_num,a.tuihuo_amount,a.tuihuo_time,b.uid,b.pay_type,b.order_type,b.order_sn,b.pay_status,b.store_id')->find();
			if($order['pay_type'] == 3){				//余额支付，返回到余额
				$res = $service->refund_credit($order,$post);	//余额退款
				$res['code'] == 1?$this->success('退款成功',''):$this->error('退款失败','');
			}	
		}else{
			$id = input('id');
			$info = Db::name('order_details')->alias('a')->join('ys_goods b','a.goods_id=b.id','LEFT')->where('a.id='.$id)->field('a.id,a.order_id,a.spec_id,a.spec_detail,a.goods_id,a.goods_num,a.give_score,a.tuihuo_sign,a.tuihuo_num,a.tuihuo_amount,a.tuihuo_time,b.name')->find();
			$this->assign('id',$id);
			$this->assign('info',$info);
			return $this->fetch('refund');
		}
	}

}